//$Id: PinYinHelper.java 2009-10-28 下午02:00:23 encoding UTF-8 $ QQ:89710328 $
package cn.com.dstz.utils;

import java.util.HashSet;
import java.util.Set;

import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;

public final class PinYinUtils {

	private PinYinUtils(){
	}
	
	/**
	 * 功能描述:     
	 * 		将汉语转换为拼音, 忽略多音字, 只返回第一个匹配      
	 * @param hanZi
	 * 			要转换的汉字
	 * @return
	 * @author ice  2009-10-28 下午02:31:14
	 */
	public static String getPinYinWithOutDuoyin(String hanZi)   {   
		
		if( hanZi == null ){
			return "";
		}
		
		String zhongWenPinYin = "";   
		char[] chars = hanZi.toCharArray();   
		
		for (int i = 0; i < chars.length; i++) {   
			try {
				 String[] pinYin = PinyinHelper.toHanyuPinyinStringArray(chars[i], getDefaultFormat());
				 // 当转换不是中文字符时,返回null   
			    if (pinYin != null) {   
			        zhongWenPinYin += pinYin[0];   
			    } else {   
			        zhongWenPinYin += chars[i];   
			    }   
			} catch (Exception e) {
				e.printStackTrace();
			}   
		}   
		
		return zhongWenPinYin;   
	}   
	/**
	 * 功能描述:
	 * 		将汉字转换成拼音, 如果有多音字, 则返回多音字列表, 以 ,号分隔            
	 * @param hanZi
	 * 			要转换的汉字
	 * @return
	 * 			转换后的拼音
	 * @author ice  2009-10-28 下午02:00:54
	 */
	public static String getPinYin(String hanZi){
		Set< String > stringSet = getPinyinCollection( hanZi );
		
		 StringBuilder builder = new StringBuilder();
		 for( String s : stringSet ){
			builder.append( s );
			break;
		 }
		 return builder.toString( ).toLowerCase( );
	}
	
	/**  
	 * 字符串集合转换字符串(逗号分隔)  
	 * @author wyh  
	 * @param stringSet  
	 * @return  
	 */  
	 public static String getPinYin(Set<String> stringSet){ 
		 
		 StringBuilder builder = new StringBuilder();
		 
		 int i = 0;
		 
		 for(String s : stringSet){
			 
			 if(i == stringSet.size() - 1){
				 builder.append(s);
			 }else{
				 builder.append(s);
				 builder.append(",");
			 }
			 
			 i++;
		 }
		 
		 return builder.toString().toLowerCase();
		 
	 }
	 
	 /**  
	  * 递归  
	  * @author wyh  
	  * @param strJaggedArray  
	  * @return  
	  */  
	  private static String[] exChange(String[][] strJaggedArray){   
	      String[][] temp = doExchange(strJaggedArray);   
	      return temp[0];          
	  }
	  
	  /**  
	   * 递归  
	   * @author wyh  
	   * @param strJaggedArray  
	   * @return  
	   */  
	  private static String[][] doExchange(String[][] strJaggedArray){   
	        int len = strJaggedArray.length;   
	        if(len >= 2){              
	            int len1 = strJaggedArray[0].length;   
	            int len2 = strJaggedArray[1].length;   
	            int newlen = len1*len2;   
	            String[] temp = new String[newlen];   
	            int Index = 0;   
	            for(int i=0;i<len1;i++){   
	                for(int j=0;j<len2;j++){   
	                    temp[Index] = strJaggedArray[0][i] + strJaggedArray[1][j];   
	                    Index ++;   
	                }   
	            }   
	            String[][] newArray = new String[len-1][];   
	            for(int i=2;i<len;i++){   
	                newArray[i-1] = strJaggedArray[i];                              
	            }   
	            newArray[0] = temp;   
	            return doExchange(newArray);   
	        }else{   
	         return strJaggedArray;      
	        }   
	    }   
	      
	 
	 /**  
	  * 获取拼音集合  
	  * @author wyh  
	  * @param src  
	  * @return Set<String>  
	  */  
	 public static Set<String> getPinyinCollection(String src){  
		 
		 if(src != null && !"".equals(src.trim())){
			 
			 char [] srcChar = src.toCharArray();
			 
			 HanyuPinyinOutputFormat pinyinFormat = getDefaultFormat();
			 
			 String [][] tempStr = new String[src.length()][];
			 
			 for(int i = 0; i < srcChar.length; i++){
				 
				 char c = srcChar[i];
				 
				 if(String.valueOf(c).matches("[\\u4E00-\\u9FA5]+")){
					 
					 try{
						 tempStr[i] = net.sourceforge.pinyin4j.PinyinHelper.toHanyuPinyinStringArray(srcChar[i], pinyinFormat); 
						 
					 }catch(BadHanyuPinyinOutputFormatCombination e) {   
					      e.printStackTrace();   
				     }  
				 }else if( (c >= 65 && c <= 90 ) || ( c >= 97 && c <= 122 ) ){  
					 tempStr[i] = new String[]{String.valueOf(srcChar[i])};   
					 
				 }else{   
					 tempStr[i] = new String[]{""};   
				 }  
			 }
			 
			 String[] pingyinArray = exChange(tempStr);   
			 Set<String> pinyinSet = new HashSet<String>();  
			 
			 for(int i=0;i<pingyinArray.length;i++){   
				 pinyinSet.add(pingyinArray[i]);   
			 }   
			 return pinyinSet;   
		 }
		 return null;
	 }
	 
	 private static HanyuPinyinOutputFormat getDefaultFormat(){
		 HanyuPinyinOutputFormat pinyinFormat = new HanyuPinyinOutputFormat();
		 pinyinFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
		 pinyinFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
		 pinyinFormat.setVCharType(HanyuPinyinVCharType.WITH_V); 
		 
		 return pinyinFormat;
	 }
	 
	 public static void main(String[] args) {
		
		 
		 String s = getPinYinWithOutDuoyin("看见你deakak");
		 
		 System.out.println(s);
	}
	  
}
